package com.soxing.webrain.brush.service.zip;

import com.soxing.webrain.brush.api.zip.IZipService;
import com.soxing.webrain.brush.common.util.ZipUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

@Slf4j
@Service("zipService")
public class ZipServiceImpl implements IZipService {

    private final static int BUFF_SIZE = 1024;

    @Override
    public void zip(String zipPath, List<File> srcFiles) throws Exception {
        long startTime = System.currentTimeMillis();
        log.info("压缩中...");

        if (CollectionUtils.isEmpty(srcFiles)) {
            throw new Exception("压缩文件为空");
        }

        ZipUtil.zipFile(zipPath, srcFiles);

        log.info("压缩完成...");
        long endTime = System.currentTimeMillis();
        log.info(zipPath + "压缩耗费时间： " + (endTime - startTime) + " ms");
    }

    @Override
    public boolean unzip(String destDir, String zipFilePath) throws Exception {
        long startTime = System.currentTimeMillis();
        int count = -1;
        File file = null;
        InputStream is = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;

        String savepath = destDir + File.separator; // 保存解压文件目录
        File saveDir = new File(savepath);
        if (!saveDir.exists()) {
            saveDir.mkdirs();
        }
        if (!new File(zipFilePath).exists()) {
            log.info(zipFilePath + "不存在");
            return false;
        }

        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(zipFilePath, Charset.forName("GBK")); // 解决中文乱码问题
            Enumeration<?> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                String filename = entry.getName();
                boolean ismkdir = false;
                if (filename.lastIndexOf("/") != -1) { // 检查此文件是否带有文件夹
                    ismkdir = true;
                }
                filename = savepath + filename;
                if (entry.isDirectory()) { // 如果是文件夹先创建
                    file = new File(filename);
                    file.mkdirs();
                    continue;
                }
                file = new File(filename);
                if (!file.exists()) { // 如果是目录先创建
                    if (ismkdir) {
                        new File(filename.substring(0, filename.lastIndexOf("/"))).mkdirs(); // 目录先创建
                    }
                }
                file.createNewFile(); // 创建文件
                is = zipFile.getInputStream(entry);
                fos = new FileOutputStream(file);
                bos = new BufferedOutputStream(fos, BUFF_SIZE);
                byte buf[] = new byte[BUFF_SIZE];
                while ((count = is.read(buf)) > -1) {
                    bos.write(buf, 0, count);
                }
                bos.flush();
                bos.close();
                fos.close();
                is.close();
            }
            zipFile.close();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return false;
        } finally {
            try {
                if (bos != null) {
                    bos.close();
                }
                if (fos != null) {
                    fos.close();
                }
                if (is != null) {
                    is.close();
                }
                if (zipFile != null) {
                    zipFile.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        long endTime = System.currentTimeMillis();
        log.info(zipFilePath + "解压耗费时间： " + (endTime - startTime) + " ms");
        return true;
    }

}
